package com.project.lk.nurseapp.home.usermsg.model;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.reflect.TypeToken;
import com.project.lk.nurseapp.base.BaseApi;
import com.project.lk.nurseapp.base.PostBaseModel;
import com.project.lk.nurseapp.utils.JsonUtils;
import com.project.lk.nurseapp.utils.ToastUtils;

import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

/**
 * 功能介绍：
 * 项目名：
 * 模块名称：
 * 作者：LK
 * 日期：2018/1/9
 * 修改日期：
 */
public class PostImageModel extends PostBaseModel<PostFileHintBean> {
    public void postLoadJson(String url,String path){

        Random random = new Random();
        int nextInt = random.nextInt();
        MultipartBody.Builder builder = new MultipartBody.Builder()
                .setType(MultipartBody.FORM);
        RequestBody body= RequestBody.create(MediaType.parse("*/image"), new File(path));
        long currentTimeMillis = System.currentTimeMillis();
        builder.addFormDataPart("filePath", path, body)
                .addFormDataPart("signature",postSignToken(currentTimeMillis+"",nextInt+""))
                .addFormDataPart("timestamp",currentTimeMillis+"")
                .addFormDataPart("nonce",nextInt+"");
        getJsonData(url,builder.build());
    }
    @Override
    public List<PostFileHintBean> analyzeJson(String json, String url) {
        Log.e("MLOG",url+json);
        try {
            JSONObject jsonObject=new JSONObject(json);
            String msg = jsonObject.getString("msg");
            String info = jsonObject.getString("info");
            if (TextUtils.equals(msg,"success")){
                JSONObject data = jsonObject.getJSONObject("data");
                List<PostFileHintBean> list=new ArrayList<>();
                PostFileHintBean bean = (PostFileHintBean) JsonUtils.getJsonUtils()
                        .toObject(new TypeToken<PostFileHintBean>(){}.getType(),data.toString());
                list.add(bean);
                ToastUtils.showShortToast(info);
                return list;
            }else {
                postImgFail();
                return null;
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("MLOG",e.getMessage());
            postImgFail();
        }
        return null;
    }



    public void postImgSuccess(){
        bridge.bridge(BaseApi.img_success,null);
    }
    public void postImgFail(){
        bridge.bridge(BaseApi.img_fail,null);
    }

    List<PostFileHintBean> mList=new ArrayList<>();

    public List<PostFileHintBean> getmList() {
        return mList;
    }

    @Override
    public void postSuccessOnMainThread(List<PostFileHintBean> list, String url) {
        if (list!=null&&list.size()!=0){
                mList.clear();
                mList.addAll(list);
                postImgSuccess();
        }else {
            postImgFail();
        }
    }

    @Override
    public void postFailedOnMainThread(String url) {
        postImgFail();
    }
}
